<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        
        $dirabc = $this->getrandmax(8);
        $dir123 = rand(0,9);
        
        //当文件夹（短链接）存在时 增加一位
        if(!is_dir($dirabc)){
            $shortdir = $dirabc;
        } else {
            $shortdir = $dirabc.$dir123;
        }
        $this->request->filter(['strip_tags', 'trim']);
        $dirpath = dirname($_SERVER['PHP_SELF']);
        $rootpath = dirname($_SERVER['PHP_SELF'])."/";
        if ($dirpath == "/"){$rootpath = "/";}
        //dump($rootpath);
        $gen = $_SERVER['DOCUMENT_ROOT'];
        if ($this->request->isPost()) {
            $origin = $this->request->post('url');
            
            if (strlen($origin) > 10 && strpos($origin, strval("http"))!==false && !is_dir($shortdir)){
                mkdir(iconv("UTF-8", "GBK", $gen.'/s/'.$shortdir),0755,true);
                $filename = $gen .'/s/'.$shortdir."/index.html";
               
                $domains = [
                    // 'y1vk9zlep2.name',
                    // '9qrv29zd2.top'  ,
                    // '9cpzltef.biz'   ,
                    // 'h9zzqforb.xyz'  ,
                    // 'sna80fryju.info',
                    'fastadmin.test'
                    
                ];
                $rand = array_rand($domains);
                $shorturl = $domains[$rand];
                $shortened = "http://".$shorturl.$rootpath.'s/'.$shortdir;
                //入库
                //原链参数
                $parsedUrl = parse_url($origin);
                //dump($parsedUrl);die;
                $data = [
                    'original_url'      => $origin,
                    'original_param'    => isset($parsedUrl['path'])?$parsedUrl['path']:'',
                    'short_url'         => $shortened,
                    'short_param'       => $shortdir,
                    'createtime'        => time()
                ];
                
                $res = db('shorturl')->insertGetId($data);
               
                file_put_contents($filename,
                '<script type="text/javascript">location.href="'.$origin.'"</script>');
                //$shortened = "http://".$_SERVER['HTTP_HOST'].$rootpath.'r/'.$shortdir;
                $info = '<p>原来的 URL：<br/><a href="'.$origin.'" target="_blank">'.$origin.'</a></p>'
                 .'<p>缩短的 URL：<br/><a href="'.$shortened.'" target="_blank">'.$shortened.'</a></p>';
                $this->assign('info',$info);
                
        
            } else {
                $info = "<p><b>您输入的URL无效！</b></p>";
                $this->assign('info',$info);
            }
        }
        
        return $this->view->fetch();
    }
    
    public function getrandmax($len){
        $chars = array(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);
        $output = "";
        for ($i=0; $i<$len; $i++){
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }

}
